﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;

namespace BTEditor
{
    public class BTMainWindows : OdinEditorWindow
    {
        private BTEditorProperty _mBtEditorAttribute;

        private BehaviourTree _mBehaviourTree;
        
        private static BTAsset mBTAsset;
        
        private Vector2 mScrollPos = Vector2.zero;
        
        [HideInInspector] public BTGrid BTGrid;
        
        public static void OpenWindows(BTAsset btAsset)
        {
            mBTAsset = btAsset;
            var window = CreateInstance<BTMainWindows>();
            window.Show();
            window.position = GUIHelper.GetEditorWindowRect().AlignCenter(BTEditorConst.WINDOWS_WIDTH, BTEditorConst.WINDOWS_WEIGHT);
        }

        protected override void OnDestroy()
        {
            if (mBTAsset!=null)
            {
                mBTAsset.Seriallized(_mBehaviourTree);
            }

            mBTAsset = null;
        }
        
        protected override IEnumerable<object> GetTargets()
        {
            
            if (_mBtEditorAttribute != null)
            {
                if (_mBtEditorAttribute.SelectNodeCount == 1)
                {
                    yield return _mBtEditorAttribute.DefaultNode.Node;
                }
            }

            yield break;
        }

        protected override void Initialize()
        {
            base.Initialize();
            if (_mBtEditorAttribute == null)
            {
                _mBtEditorAttribute = new BTEditorProperty();
                BTEditorProperty.Instance = _mBtEditorAttribute;
            }
            if (mBTAsset!=null && mBTAsset.IsHaveData())
            {
                _mBehaviourTree = mBTAsset.DeSeriallized();
                _mBtEditorAttribute.BehaviourTree = _mBehaviourTree;
                _mBtEditorAttribute.Position = Vector2.zero;
            }
            else
            {
                if (_mBehaviourTree == null)
                {
                    _mBehaviourTree = new BehaviourTree(); 
                    _mBehaviourTree.CreateRoot();
                    _mBtEditorAttribute.BehaviourTree = _mBehaviourTree;
                }
            }

            _mBtEditorAttribute.CurAsset = mBTAsset;
      
            if (BTGrid == null)
            {
                BTGrid = new BTGrid();
            }
            
            GetBehaveClass();
        }
        
        
        private void GetBehaveClass()
        {
            Type[] types = typeof(EditorNode).Assembly.GetTypes();
            for (int i = 0; i < types.Length; i++)
            {
                Type temp = types[i];

                if (temp.GetCustomAttribute<BTCompositeAttribute>() != null)
                {
                    _mBtEditorAttribute.CompositeDic.Add(BTUtils.GetTypeName(temp.FullName), temp);
                }
                else if (temp.GetCustomAttribute<BTTaskAttribute>() != null)
                {
                    _mBtEditorAttribute.TaskDic.Add(BTUtils.GetTypeName(temp.FullName), temp);
                }
                else if (temp.GetCustomAttribute<BTDecoratorAttribute>() != null)
                {
                    _mBtEditorAttribute.DecoratorDic.Add(BTUtils.GetTypeName(temp.FullName), temp);
                }
//                else if (temp.GetCustomAttribute<BTActionAttribute>() != null)
//                {
//                    _mBtEditorAttribute.ActionDic.Add(BTUtils.GetTypeName(temp.FullName), temp);
//                }
            }
        }

        protected override void OnGUI()
        {
            BTGrid.DrawGrid(position.size);
            //绘制最上面的Tools工具栏
            SirenixEditorGUI.BeginHorizontalToolbar(24);
            {
                GUILayout.Label("Tools");
                GUIHelper.PushContentColor(GUI.contentColor);
                GUI.contentColor = _mBehaviourTree.IsAutoSave ? Color.green : Color.red;
                _mBehaviourTree.IsAutoSave = SirenixEditorGUI.ToolbarToggle(_mBehaviourTree.IsAutoSave,"是否自动保存");
                GUIHelper.PopContentColor();
                if (SirenixEditorGUI.ToolbarButton(new GUIContent("返回中心点")))
                {
                    _mBtEditorAttribute.Position = Vector2.zero;
                }
                if (SirenixEditorGUI.ToolbarButton(new GUIContent("生成代码")))
                {
                    ConvertToCode();
                }
                if (SirenixEditorGUI.ToolbarButton(new GUIContent("保存修改")))
                {
                    mBTAsset.Seriallized(_mBehaviourTree);
                    Debug.Log("保存成功");
                }              
            }
            SirenixEditorGUI.EndHorizontalToolbar();
            
            GUILayout.BeginHorizontal();

            GUILayout.BeginVertical();
            {
                _mBehaviourTree.Update(position);
            }
            GUILayout.EndVertical();

            GUILayout.BeginVertical(GUILayoutOptions.Width(BTEditorConst.LEFT_INSPECT_WIDTH).ExpandWidth());
            {
                mScrollPos = GUILayout.BeginScrollView(mScrollPos, false, true);
                var rect = GUIHelper.GetCurrentLayoutRect();
                EditorGUI.DrawRect(rect, SirenixGUIStyles.DarkEditorBackground);
                SirenixEditorGUI.BeginBox();
                {
                    GUILayout.Space(5);                  
                    SirenixEditorGUI.Title("编辑器管理", "", TextAlignment.Center, true);
                    GUILayout.Label("节点树名字：",SirenixGUIStyles.Label);
                    _mBehaviourTree.TreeFileName =
                        GUILayout.TextField(_mBehaviourTree.TreeFileName, SirenixGUIStyles.CenteredTextField);
                }
                SirenixEditorGUI.EndBox();

                SirenixEditorGUI.BeginBox();
                {
                    SirenixEditorGUI.Title("节点属性", "", TextAlignment.Center, true);
                    base.OnGUI();
                }
                SirenixEditorGUI.EndBox();
                GUILayout.EndScrollView();
            }
            GUILayout.EndVertical();

            GUILayout.EndHorizontal();    
        }
        
        private void ConvertToCode()
        {
            StringBuilder sb = new StringBuilder();
            BTNode rootBTNode = _mBehaviourTree.root;
            sb.Append(rootBTNode.Node.ToChild());
            File.WriteAllText("test.txt", sb.ToString());
            EditorUtility.DisplayDialog("生成代码成功", "", "ok");
        }
    }
}


